package com.zh.note.huawei.onehundred.选修课;

import java.util.*;
import java.util.function.Function;

/**
 * @Author: zhanghuan
 * @date: 2023/8/15 14:53
 * @description: create it
 */
public class Main {
    static class Student {
        private String id;
        private int score;

        public Student(String id, int socre) {
            this.id = id;
            this.score = socre;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }
    }

    /**
     * 01202021,75;01201033,95;01202008,80;01203006,90;01203088,100
     * 01202008,70;01203088,85;01202111,80;01202021,75;01201100,88
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String str1 = sc.nextLine();
        // String str2 = sc.nextLine();
        String str1 = "01202021,75;01201033,95;01202008,80;01203006,90;01203088,100";
        String str2 = "01202008,70;01203088,85;01202111,80;01202021,75;01201100,88";
        String[] one = str1.split(";");
        String[] two = str2.split(";");
        Map<String, Integer> secondStudentMap = new HashMap<>();
        for (String t : two) {
            String[] stu = t.split(",");
            String id = stu[0];
            int score = Integer.parseInt(stu[1]);
            secondStudentMap.put(id, score);
        }
        //key是班级
        Map<String, TreeSet<Student>> map = new TreeMap<>();

        for (String s : one) {
            String[] stu = s.split(",");
            String id = stu[0];
            if (secondStudentMap.containsKey(id)) {
                int firstScore = Integer.parseInt(stu[1]);
                int secondScore = secondStudentMap.get(id);
                int totalScore = firstScore + secondScore;
                //获取班级
                String cls = id.substring(0, 5);
                Student student = new Student(id, totalScore);
                // 先按照分数降序排列，分数相同则按照学号升序，
                map.computeIfAbsent(cls, k -> new TreeSet<>(Comparator.comparing(new Function<Student, Integer>() {
                    @Override
                    public Integer apply(Student student) {
                        return student.getScore();
                    }
                }).reversed().thenComparing(Student::getId))).add(student);
            }
        }
        if (map.isEmpty()) {
            System.out.println("NULL");
        } else {
            map.forEach((key, value) -> {
                System.out.println(key);
                String[] array = value.stream().map(Student::getId).toArray(String[]::new);
                String res = String.join(";", array);
                System.out.println(res);
            });
        }

    }
}
